设置用户信息

Git 要求使用者必须提供自己的身份标识,为此我们需要在 Git bash中执行以下命令(当然不设置也行,每次提交都是 unknown… macOS 貌似会提交系统用户名):

//给自己起个用户名
git config --global user.name "name"
//填写自己的邮箱
git config --global user.email "abc@gmail.com"

检查旧密钥

生成密钥之前先检测是否存在旧密钥

# Windows 目录在 C:\Users\user\.ssh
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

默认情况下,公钥的文件名是以下之一

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

如果你没有现有的公钥和私钥对,或者不希望使用任何可用于连接到 Github 的公钥和私钥,请生成一个新的 ssh 密钥

如果您看到列出的现有公钥和私钥对(例如 id_rsa.pub 和 id_rsa ),那么你可以将 ssh 密钥添加到 ssh-agent 中

生成密钥

以下步骤可能会过时,可参考 https://help.github.com/articles/connecting-to-github-with-ssh/ 获取更详细步骤

GitHub 选择的默认通信方式是 SSH,如果没有配置 SSH key 就只能 clone 代码不能 push 上去(只读),所以要先在 git 里面生成 SHH Key。如果在当前用户的第一级文件夹下有 .ssh 文件夹,说明以前可能使用过 git,把该文件夹删除(或者先备份再删除,防止意外)。

获得密钥:

ssh-keygen -t rsa -b 4096 -C "i@gmail.com"

添加密钥到 ssh-agent

添加刚刚生成的证书

  1. 确保ssh-agent正在运行

    # start the ssh-agent in the background
    eval $(ssh-agent -s)
    Agent pid 59566
  2. 添加证书

    ssh-add ~/.ssh/id_rsa

添加密钥到 GitHub

  1. 将 ssh 密钥复制到剪贴板

    pbcopy < ~/.ssh/id_rsa.pub
    # Copies the contents of the id_rsa.pub file to your clipboard
  2. 打开 Settings,在用户设置边栏中,单击 SSH and GPG keys ,单击 New SSH key or Add SSH key ,”Title” 随意填写,如“Personal MacBook Pro”,“Key” 填写刚刚复制过的文本,直接粘贴

  3. 最后测试下

    ssh -T git@github.com

    提示像我这样:Hi XXX! You’ve successfully authenticated, but GitHub does not provide shell access. 那就说明连接成功了。

设置代理

git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

隐藏 commits 记录的邮箱地址

如果你想隐藏你的 git log 中的邮箱,但同时 contributions 也不会消失,那么可以采取以下的方法

  1. 设置页面 勾选 Keep my email address private

    这时候,会给你一个的电子邮件 yourusername@users.noreply.github.com 供你用于你的 Git 提交。

  2. 更改本地 git config 中的 user.email

    git config --global user.email  "yourusername@users.noreply.github.com" 
    git config --global user.email
  3. 使用脚本更改 Git 仓库的 commits 历史记录

    • 打开 Git Bash

    • 克隆你要修改的 repo

      git clone --bare https://github.com/user/repo.git
      cd repo.git
    • 复制以下脚本,填写好信息,然后执行脚本

      #!/bin/sh

      git filter-branch --env-filter '
      OLD_EMAIL="填写你的旧邮箱"
      CORRECT_NAME="填写你的 Github 用户名"
      CORRECT_EMAIL="填写新邮箱"
      if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
      then
      export GIT_COMMITTER_NAME="$CORRECT_NAME"
      export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
      fi
      if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
      then
      export GIT_AUTHOR_NAME="$CORRECT_NAME"
      export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
      fi
      ' --tag-name-filter cat -- --branches --tags
    • 运行脚本,等待完成

    • 提交历史 git push --force --tags origin 'refs/heads/*'

    • 完成,可以删除 repo

参考:

https://help.github.com/articles/connecting-to-github-with-ssh/

https://saraford.net/2017/02/19/how-to-hide-your-email-address-in-your-git-commits-but-still-get-contributions-to-show-up-on-your-github-profile-050/